package com.first.core.util;

import java.io.Serializable;

public class Page
  implements Serializable
{
  private static final long serialVersionUID = 7346076082033400525L;
  public static final int PAGESIZE = 20;
  private int totalRowsAmount;
  private boolean rowsAmountSet;
  private int pageSize = 20;

  private int currentPage = 1;
  private int nextPage;
  private int previousPage;
  private int totalPages;
  private boolean hasNext;
  private boolean hasPrevious;
  private int pageStartRow;
  private int pageEndRow;

  public Page(int totalRows)
  {
    setTotalRowsAmount(totalRows);
  }

  public Page()
  {
  }

  public void setTotalRowsAmount(int i)
  {
    if (this.pageSize == 0) {
      this.pageSize = 20;
    }
    if (!this.rowsAmountSet) {
      this.totalRowsAmount = i;
      this.totalPages = (this.totalRowsAmount % this.pageSize == 0 ? this.totalRowsAmount / this.pageSize : 
        this.totalRowsAmount / this.pageSize + 1);
      setCurrentPage(this.currentPage);
      this.rowsAmountSet = true;
    }
  }

  public void setCurrentPage(int i)
  {
    this.currentPage = i;
    this.nextPage = (this.currentPage + 1);
    this.previousPage = (this.currentPage - 1);
    if (this.currentPage * this.pageSize < this.totalRowsAmount) {
      this.pageEndRow = (this.currentPage * this.pageSize);
      this.pageStartRow = (this.pageEndRow - this.pageSize + 1);
    } else {
      this.pageEndRow = this.totalRowsAmount;
      this.pageStartRow = (this.pageSize * (this.totalPages - 1) + 1);
      if (this.pageStartRow < 0) {
        this.pageStartRow = 0;
      }
    }
    if (this.nextPage > this.totalPages)
      this.hasNext = false;
    else {
      this.hasNext = true;
    }
    if (this.previousPage == 0)
      this.hasPrevious = false;
    else
      this.hasPrevious = true;
  }

  public int getCurrentPage()
  {
    return this.currentPage;
  }

  public boolean isHasNext() {
    return this.hasNext;
  }

  public boolean isHasPrevious() {
    return this.hasPrevious;
  }

  public int getNextPage() {
    return this.nextPage;
  }

  public int getPageSize() {
    return this.pageSize;
  }

  public int getPreviousPage() {
    return this.previousPage;
  }

  public int getTotalPages() {
    return this.totalPages;
  }

  public int getTotalRowsAmount() {
    return this.totalRowsAmount;
  }

  public void setHasNext(boolean b) {
    this.hasNext = b;
  }

  public void setHasPrevious(boolean b) {
    this.hasPrevious = b;
  }

  public void setNextPage(int i) {
    this.nextPage = i;
  }

  public void setPageSize(int i) {
    this.pageSize = i;
  }

  public void setPreviousPage(int i) {
    this.previousPage = i;
  }

  public void setTotalPages(int i) {
    this.totalPages = i;
  }

  public int getPageEndRow() {
    return this.pageEndRow;
  }

  public int getPageStartRow() {
    return this.pageStartRow;
  }

  public void setPageStartRow(int pageStartRow) {
    this.pageStartRow = pageStartRow;
  }

  public void setPageEndRow(int pageEndRow)
  {
    this.pageEndRow = pageEndRow;
  }

  public String getDescription() {
    String description = "Total:" + getTotalRowsAmount() + " items " + 
      getTotalPages() + " pages,Current page:" + this.currentPage + 
      " Previous " + this.hasPrevious + " Next:" + this.hasNext + 
      " start row:" + this.pageStartRow + " end row:" + this.pageEndRow;
    return description;
  }

  public static void main(String[] args) {
    Page pc = new Page();

    pc.setPageSize(4);
    pc.setTotalRowsAmount(8);
    pc.setCurrentPage(1);
  }
}